package org.devocean.groupbuying.web.backend;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.CategoriesLocalization;
import org.devocean.groupbuying.domain.Category;
import org.devocean.groupbuying.service.CategoriesLocalizationService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/gb-cpanel/categoriesLocalizations" })
public class CategoriesLocalizationController {

	@Autowired
	private CategoriesLocalizationService categoriesLocalizationService;


	/**
	 * Edit a category
	 */
		
	@RequestMapping(value = "/{categoryId}", method = RequestMethod.GET)
	public Map<String, ? extends Object> edit(@PathVariable("categoryId") Integer categoryId) {
		try {
			CategoriesLocalization categoriesLocalization = categoriesLocalizationService.getCategoryById(categoryId);
			return renderCategoryLocalizationToEdit(categoriesLocalization, 0,"");

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error retrieving Categories from database.");
		}
	}

	/**
	 * Add or update a category
	 */
	
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, ? extends Object> addOrUpdate(@RequestBody CategoriesLocalization categoriesLocalization) {
		try {
			CategoriesLocalization newCategoriesLocalization = categoriesLocalizationService.addOrUpdateCategoriesLocalization(categoriesLocalization);
			return RenderMapUtils.getMap(renderCategoryLocalizationToEdit(newCategoriesLocalization,0,""));
		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error saving Category to database.");
		}		
	}	
	/**
	 * edit other language for a category
	 */
	
	@RequestMapping(value = "/localize", method = RequestMethod.GET)
	public Map<String, ? extends Object> localize(Integer LocalizationKey, String LocalizationRequest, Integer activeElementId) {
		try {
			CategoriesLocalization categoriesLocalization = categoriesLocalizationService.getCategoriesLocalizationById(LocalizationKey);			
			
			for(Category category : categoriesLocalization.getCategories()){
				if(category.getLang().equals(LocalizationRequest)){
					categoriesLocalization.getCategories().clear();
					categoriesLocalization.getCategories().add(category);					
					return renderCategoryLocalizationToEdit(categoriesLocalization, 0,"");
				}
			}			
			Iterator<Category> iterator = categoriesLocalization.getCategories().iterator();
			Category parentCategory = iterator.next().getCategory();
			String model = parentCategory.getModel();
			CategoriesLocalization parentCategoriesLocalization = parentCategory.getCategoriesLocalization();
			//find parent with the same language
			int parent = -1;
			for(Category category : parentCategoriesLocalization.getCategories()){
				if(category.getLang().equals(LocalizationRequest)){
					parent = category.getCategoryID();
				}
			}
						
			categoriesLocalization.getCategories().clear();

			return renderCategoryLocalizationToEdit(categoriesLocalization, parent,model);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error retrieving Category from database.");
		}

	}

	private Map<String, ? extends Object> renderCategoryLocalizationToEdit(CategoriesLocalization categoriesLocalization, int parent, String model) {
		Map<String, Object> data = new ConcurrentHashMap<String, Object>();
		Set<Category> categories = (Set<Category>) categoriesLocalization.getCategories();
		
		data.put("categoriesLocalizationID", categoriesLocalization.getCategoriesLocalizationID());
		if(parent > 0){
			data.put("parentCategoryID",parent);
		}
		else
			data.put("flag",parent == 0 ? true : false);
		
		if(model.length() > 0)
			data.put("model", model);
		if (categories.size() > 0) {
			Iterator<Category> iterator = categories.iterator();
			Category category = iterator.next();
			data.put("title", category.getName()); 
			data.put("name", category.getName());
			data.put("lang", category.getLang());
			data.put("model", category.getModel());
			data.put("categoryID", category.getCategoryID());
			data.put("parentCategoryID", category
					.getCategory() == null ? 0 : category
					.getCategory().getCategoryID());
		}

		return RenderMapUtils.getMap(data);
	}

}
